package com.woniu.microworkorderserver.util;

import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSS;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class UploadImg {

    public static String getUploadImgUrl(MultipartFile file){
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-chengdu.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，
        // 创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        // 非支付宝密码
        // 子账户密码，需设置
        String accessKeyId = "LTAI4GEaTxk2gJA4fF5QTMYu";
        String accessKeySecret = "T9gj4FAFsoqpqyalEftbdVDVWPQacW";
        String bucketName = "liaojian-oss";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        PutObjectResult result;
        try {
            InputStream is = file.getInputStream();
            result = ossClient.putObject(bucketName,"file",is);
            System.out.println(result.getResponse().getUri());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //关闭Ossclient
        ossClient.shutdown();
        return "上传成功";
    }
}
